package 题目集.栈.计算器;

import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
import java.util.Stack;

public class 模拟计算器 {
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        String line = sc.nextLine();
        String[] split = line.split("\\D");
        int[] nums=new int[split.length];
        for (int i = 0; i < split.length; i++) {
            nums[i]=Integer.parseInt(split[i]);
        }
        split = line.split("\\d");
        char symbol[]=new char[split.length-1];
        for (int i = 0; i < split.length-1; i++) {
            symbol[i]=split[i+1].charAt(0);
        }
        System.out.println(handler(nums,symbol));
    }

    public static int handler(int[] nums,char[] symbol){
        Stack<Integer> numberStack=new Stack<>();
        Stack<Character> symbolStack=new Stack<>();
        int i=0,j=0;
        numberStack.push(nums[i++]);
        numberStack.push(nums[i++]);
        symbolStack.push(symbol[j++]);
        while (i<nums.length&&j<symbol.length){
            while (!symbolStack.isEmpty()&&map.get(symbol[j])<=map.get(symbolStack.peek())){    //如果之前的符号优先级高，则先计算之前的
                Integer right = numberStack.pop();
                Integer left = numberStack.pop();
                int res=calculate(left,right,symbolStack.pop());
                numberStack.push(res);
            }
            numberStack.push(nums[i++]);
            symbolStack.push(symbol[j++]);
        }
        while (!symbolStack.isEmpty()){
            int res=calculate(numberStack.pop(),numberStack.pop(),symbolStack.pop());
            numberStack.push(res);
        }
        return numberStack.pop();
    }

    static Map<Character,Integer> map=new HashMap<>();
    static {
        map.put('+',1);
        map.put('-',1);
        map.put('*',2);
        map.put('/',2);
    }
    public static int calculate(int left,int right,char symbol){
        switch (symbol){
            case '+':
                return left+right;
            case '-':
                return left-right;
            case '*':
                return left*right;
            case '/':
                return left/right;
            default:
                return 0;
        }
    }
}
